package cn.sstir.hackathon.core.search;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;

/**
 * 基础查询条件
 *
 * @author: fli
 * @email: fli@sstir.cn
 * @date: 2019/12/25 18:09
 */
@Getter
public class BaseQuery {


    private static final int MAX_PAGE_NO = 100;

    private static final int DEFAULT_PAGE_NO = 1;

    private static final int DEFAULT_PAGE_SIZE = 50;

    /**
     * 关键词
     */
    @Setter
    private String keyword;
    /**
     * 页码
     */
    private Integer pageNo = DEFAULT_PAGE_NO;
    /**
     * 每页展示条数
     */
    @Setter
    private Integer pageSize = DEFAULT_PAGE_SIZE;

    /**
     * 排序字段
     */
    @Setter
    private String sortField;

    /**
     * 排序方式:降序或升序(asc/desc)
     */
    private String sortOrder = "desc";


    /**
     * 设置排序方式
     *
     * @author fli
     * @date 2020/1/2 17:47
     */
    public void setSortOrder(String sortOrder) {
        if (Arrays.asList("asc", "desc").contains(sortOrder)) {
            this.sortOrder = sortOrder;
        } else {
            this.sortOrder = "desc";
        }
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = Math.min(Math.max(DEFAULT_PAGE_NO,pageNo),MAX_PAGE_NO);
    }
}
